package at.ac.tuwien.ifs.calendit.basis.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import at.ac.tuwien.ifs.calendit.basis.gui.NEWTabUebersichtsPanel.UpdateListener;
import at.ac.tuwien.ifs.calendit.domain.Meeting;
import at.ac.tuwien.ifs.calendit.domain.Termin;


public class EDITTabUebersichtsPanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel UebersichtPanel;
	private JPanel ContentPanel;
	//private JRadioButton rb_Meeting;
	private JRadioButton rb_Termin;
	private JRadioButton rb_Todo;
	private EditTerminPanel editTerminPanel_IL;
	private EditTerminPanel editTerminPanel_IL1;
	private TODOTableModel todoTM;
	
	/**
	 * Retrieves the logger for this class.
	 */
	private static Log log = LogFactory.getLog(EDITTabUebersichtsPanel.class);
	
//	/**
//	 * Enumeration of different Modes the EditTODOFrame can be used with.
//	 * Possible modes are:
//	 * <ul>
//	 * <li><code>Create</code> for creating a new todo.</li>
//	 * <li><code>Update</code> for updating an existing todo.</li>
//	 * </ul>
//	 * 
//	 * @author The SE-Team
//	 * @see EditTODOFrame#EditTODOFrame(todo, Mode)
//	 */
//	public enum Mode {
//		/**
//		 * Use this mode to create a todo.
//		 */
//		Create,
//
//		/**
//		 * Use this mode to update a todo.
//		 */
//		Update,
//	};
//
//	/**
//	 * The current mode of operation, gets set in constructor.
//	 * 
//	 * @see EditTODOFrame#EditTODOFrame(todo, Mode)
//	 */
//	private Mode mode;

	
	
	private String typ; //entweder Termin, Meeting oder ToDo
	private Termin t;
	private Meeting m;
	private String mod;
	
	
	
	/**
	* Default-Constructor to load the GUI 
	*/

	public EDITTabUebersichtsPanel() {
		super();

		initModels();
		log.info("EDITTabUebersichtsPanel");

		initGUI();
	}
	
	/**
	* Constructor to specify the selected RadioButton and to load the 
	* according Panel 
	* @param: String selected
	*/
	public EDITTabUebersichtsPanel(String selected) {
		
		super();

		initModels();
		initGUI();
		if(selected.equals("termin")){
			rb_Termin.setSelected(true);
			rb_Todo.setEnabled(false);
		}
//		if(selected.equals("meeting"))
//			rb_Meeting.setSelected(true);
		if(selected.equals("todo")){
			rb_Todo.setSelected(true);
			rb_Termin.setEnabled(false);
		}
		System.out.println("dada");
		repaint();
	}
	public EDITTabUebersichtsPanel(Termin _t, String m){
		super();
		this.t = _t;
		//this.mod = m;
		initModels();
		initGUI();
		//rb_Termin.setSelected(true);
		typ = "termin";
		repaint();
	}
	public EDITTabUebersichtsPanel(Termin _t){
		super();
		this.t = _t;
		initModels();
		initGUI();
		rb_Termin.setSelected(true);
		typ = "termin";
		repaint();
	}
	
	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			this.setLayout(thisLayout);
			this.setBackground(Color.white);
			this.setBorder(BorderFactory.createLineBorder( Color.orange));
		//	setPreferredSize(new Dimension(400, 300));
			{
				UebersichtPanel = new JPanel();
				ContentPanel = new JPanel();
				BorderLayout contentLayout = new BorderLayout();
				
				ContentPanel.setLayout(contentLayout);
			}{
				
				ContentPanel.removeAll();

				this.add(ContentPanel, BorderLayout.CENTER);
				{
//					editTerminPanel_IL = new EditTerminPanel(t,EditTerminPanel.Mode.Bearbeiten);
//					ContentPanel.add(editTerminPanel_IL, BorderLayout.CENTER);
//					editTerminPanel_IL.setBackground(Color.white);
				}
				{
//					editTerminPanel_IL1 = new EditTerminPanel(t,EditTerminPanel.Mode.Bearbeiten);
//					ContentPanel.add(editTerminPanel_IL1,BorderLayout.CENTER);
//					editTerminPanel_IL1.setPreferredSize(new java.awt.Dimension(400, 396));
				}

				validate();
				
			}
				ButtonGroup group= new ButtonGroup();
				this.add(UebersichtPanel, BorderLayout.NORTH);
				
				/**
				  *  Generate RadioButtons and Register a listener for the radio buttons.
				  */
				
				UebersichtPanel.setBackground(new java.awt.Color(255,255,255));
				{
					rb_Termin = new JRadioButton();
					rb_Termin.setSelected(true);
					UebersichtPanel.add(rb_Termin);
					rb_Termin.setText("Termin");
					rb_Termin.setBackground(new java.awt.Color(255,255,255));
					rb_Termin.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try {
								changeContent(evt); 
							} catch (ParseException e) {
								// TODO Auto-generated catch bdlock
								e.printStackTrace();
							}
						}
					});
					group.add(rb_Termin);
				}

				{
					rb_Todo = new JRadioButton();
					UebersichtPanel.add(rb_Todo);
					rb_Todo.setText("Todo");
					rb_Todo.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try {
								changeContent(evt);
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
					
					rb_Todo.addChangeListener(new ChangeListener() {

						public void stateChanged(ChangeEvent e) {
							// TODO Auto-generated method stub
							try {
								changeContentTodo(e);
							} catch (ParseException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							
						}
					});
					
					group.add(rb_Todo);
					rb_Todo.setBackground(new java.awt.Color(255,255,255));

					rb_Termin.setVisible(false);
					rb_Todo.setVisible(false);
	
				}
				//this.add(ContentPanel, BorderLayout.CENTER);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Initializes the Models. Creates the TODOTableModel (for the Table)
	 * 
	 * @see TODOTableModel
	 */
	private void initModels() {
		todoTM = new TODOTableModel("aufgabeId");
	}
	
	private void changeContent(ActionEvent evt) throws ParseException {
		System.out.println("rb_Termin.actionPerformed, event="+evt);
		//TODO add your code for rb_Termin.actionPerformed
		System.out.println(evt.getActionCommand());
		if(evt.getActionCommand().equals("Termin"))	{
			ContentPanel.removeAll();
			ContentPanel.add(new EditTerminPanel(t, EditTerminPanel.Mode.Bearbeiten),BorderLayout.CENTER);
			this.add(ContentPanel, BorderLayout.CENTER);

			validate();
			
			try{
			ChangeListener[] ch= rb_Todo.getChangeListeners();
			rb_Todo.removeChangeListener(ch[0]);
			}catch(Exception e){}
			
		}
		 else
				try {
					if(evt.getActionCommand().equals("Todo")){
						System.out.println("todo actionlistener");
						ContentPanel.removeAll();
						int row = TODOVerwaltungFrame.getTable().getSelectedRow();
						EditTODOFrame emf = new EditTODOFrame(TODOVerwaltungFrame.todoTM.getTODOAt(row),EditTODOFrame.Mode.Bearbeiten);
						ContentPanel.add(emf,BorderLayout.CENTER);
						
						emf.validate();
						emf.addUpdateListener(new UpdateListener());
						emf.setVisible(true);
						this.add(ContentPanel, BorderLayout.CENTER);
						
						try{
							ChangeListener[] ch= rb_Termin.getChangeListeners();
							rb_Termin.removeChangeListener(ch[0]);
							}catch(Exception e){}
						
						validate();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				

		}
	private void changeContentTodo(ChangeEvent evt) throws ParseException {
		System.out.println("rb_Termin.actionPerformed, event="+evt);
		//TODO add your code for rb_Termin.actionPerformed
		System.out.println("chagenContent"+evt.getSource());
		System.out.println("getclass"+ evt.getClass());

					System.out.println("todo actionlistener");
					ContentPanel.removeAll();
					int row = TODOVerwaltungFrame.getTable().getSelectedRow();
					EditTODOFrame emf = new EditTODOFrame(TODOVerwaltungFrame.todoTM.getTODOAt(row),EditTODOFrame.Mode.Bearbeiten);
					ContentPanel.add(emf,BorderLayout.CENTER);
					
					emf.validate();
					emf.addUpdateListener(new UpdateListener());
					emf.setVisible(true);
					this.add(ContentPanel, BorderLayout.CENTER);

					validate();
					try{
						ChangeListener[] ch= rb_Termin.getChangeListeners();
						rb_Termin.removeChangeListener(ch[0]);
						}catch(Exception e){}
			
		}
	
	class UpdateListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			log.info("Update todoTM");
			todoTM.reload();
		}
	}
		
	}
	